package henu.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import henu.bean.FeedBack;
import henu.bean.Lesson;
import henu.factory.DaoFactory;

public class LessonAction extends ActionSupport{
	public String add(){
		HttpServletRequest request = ServletActionContext.getRequest();
		int n = 0;
		Lesson lesson = new Lesson();
		String name = request.getParameter("name");
		String num = request.getParameter("num");
		Integer theoryTime = Integer.parseInt(request.getParameter("teachingweek"));
		String testTime = request.getParameter("doweek");
		String startweek = request.getParameter("startweek");
		String endweek = request.getParameter("endweek");
		String year = request.getParameter("year");
		String term = request.getParameter("term");
		String remark = request.getParameter("remark");
		lesson.setfClassName(name);
		lesson.setfClassNumber(num);
		lesson.setfTheoryTime(theoryTime);
		lesson.setfTestTime(testTime);
		lesson.setfStartWeek(startweek);
		lesson.setfEndWeek(endweek);
		lesson.setfSchoolYear(year);
		lesson.setfSchoolTerm(term);
		lesson.setfRemark(remark);
		n = DaoFactory.createLessonDao().add(lesson);
		System.out.println(n+"aaaaa");
		if(n == 1){
			return "addsuccess";
		}else{
			return ERROR;
		}
	}
	
	public String getAll(){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		List<Lesson> lessons = DaoFactory.createLessonDao().getAll();
		List<FeedBack> fbs = DaoFactory.createFeedBackDao().getAll();
		if(lessons != null){
			session.setAttribute("lessons", lessons);
			session.setAttribute("fbs", fbs);
			return "getAll";
		}else{
			return ERROR;
		}
	}
	
	public String getAllByPage(){
		HttpServletRequest request = ServletActionContext.getRequest();
		System.out.println("page in");
		List<Lesson> lens = null;
		int pageNow = Integer.parseInt(request.getParameter("num"));
		System.out.println("pageNow"+pageNow);
		lens = DaoFactory.createLessonDao().getAll(5,pageNow);
		System.out.println(lens.size());
		int count = (int) DaoFactory.createLessonDao().getCount();
		System.out.println(count);
		if(count%5==0){
			request.getSession().setAttribute("pageCount", count/5);
		}else{
			request.getSession().setAttribute("pageCount", (count/5)+1);
		}
		request.getSession().setAttribute("lessons", lens);
		request.getSession().setAttribute("pageNow", pageNow);
	
		return "getAll";
	}
	
	public String delete(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("fClassNumber");
		int n = DaoFactory.createLessonDao().delete(id);
		if(n == 1){
			return "delete";
		}else{
			return ERROR;
		}
	}
	
	public String search(){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		
		String method = request.getParameter("method");
		if(method.equals("id")){
			String id = request.getParameter("keyword");
			List<Lesson> lessons = DaoFactory.createLessonDao().getById(id);	
			session.setAttribute("lessons", lessons);
		}
		if(method.equals("name")){
			String name = request.getParameter("keyword");
			List<Lesson> lessons = DaoFactory.createLessonDao().getByName(name);	
			session.setAttribute("lessons", lessons);
		}
		if(method.equals("year")){
			String year = request.getParameter("keyword");
			List<Lesson> lessons = DaoFactory.createLessonDao().getByYear(year);	
			session.setAttribute("lessons", lessons);
		}
		return "search";
	}
	
	public String update(){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession(); 
		String fClassNumber = request.getParameter("fClassNumber");
		Lesson lesson = DaoFactory.createLessonDao().get(fClassNumber);
		if(lesson != null){
			session.setAttribute("lesson", lesson);
			return "update";
		}else{			
			return ERROR;
		}
	}
	
	public String updateInfo(){
		HttpServletRequest request = ServletActionContext.getRequest();
		int n = 0;		
		String name = request.getParameter("name");
		String num = request.getParameter("num");
		Integer theoryTime = Integer.parseInt(request.getParameter("teachingweek"));
		String testTime = request.getParameter("doweek");
		String startweek = request.getParameter("startweek");
		String endweek = request.getParameter("endweek");
		String year = request.getParameter("year");
		String term = request.getParameter("term");
		String remark = request.getParameter("remark");
		Lesson lesson = new Lesson();
		lesson.setfClassName(name);
		lesson.setfClassNumber(num);
		lesson.setfTheoryTime(theoryTime);
		lesson.setfTestTime(testTime);
		lesson.setfStartWeek(startweek);
		lesson.setfEndWeek(endweek);
		lesson.setfSchoolYear(year);
		lesson.setfSchoolTerm(term);
		lesson.setfRemark(remark);
		n = DaoFactory.createLessonDao().update(lesson);
		if(n == 1){
			return "updateInfo";
		}else{
			return ERROR;
		}
	}
}
